/**
 * <b>项目名称：</b>网络应用框架<br/>
 * <b>包    名：</b>com.hhwy.framework.util<br/>
 * <b>文 件 名：</b>WebXmlUtil.java<br/>
 * <b>版本信息：</b><br/>
 * <b>日    期：</b>2016年9月9日-下午3:40:04<br/>
 * <b>Copyright (c)</b> 2016恒华伟业科技股份有限公司-版权所有<br/>
 * 
 */
package com.hhwy.framework.util;

import java.io.File;
import java.io.StringBufferInputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hhwy.framework.common.Constants;

 /**
 * <b>类 名 称：</b>WebXmlUtil<br/>
 * <b>类 描 述：</b><br/>
 * <b>创 建 人：</b>liuhai<br/>
 * <b>修 改 人：</b>liuhai<br/>
 * <b>修改时间：</b>2016年9月9日 下午3:40:04<br/>
 * <b>修改备注：</b><br/>
 * @version 1.0.0<br/>
 */
@SuppressWarnings("deprecation")
public class WebXmlUtil {

	public static final Logger log = LoggerFactory.getLogger(WebXmlUtil.class);
	public static final String webxmlPath = "/WEB-INF/web.xml";
	public static final String excludeFilterJSPListParamName = "exclude-filter-jsp-list";
	public static final String URL_SPLIT_PATTERN = "[, ;"+System.lineSeparator()+"]";//逗号  空格 分号  换行
	private static List<String> welcomePages = new ArrayList<>();
	private static List<String> errorPages = new ArrayList<>();
	private static List<String> excludePages = new ArrayList<>();
	static{
		try {
			SAXReader reader = new SAXReader();
			Document document;
			if(null != Constants.TOMCAT_MERGEDWEBXML){
				document = reader.read(new StringBufferInputStream(Constants.TOMCAT_MERGEDWEBXML));
			}else{
				String path = IoTools.getABSPATH() + webxmlPath;
				document = reader.read(new File(path));
			}
			
			Element rootElement = document.getRootElement();
			
			String pagePath = "";
			
			//欢迎页列表
			Element welcomeFileList = rootElement.element("welcome-file-list");
			if(welcomeFileList != null){
				@SuppressWarnings("unchecked")
				List<Element> welcomeFileElements = welcomeFileList.elements("welcome-file");
				for(Element element : welcomeFileElements){
					pagePath = element.getTextTrim();
					if(StringUtils.isNotBlank(pagePath)){
						if(!pagePath.startsWith("/"))
							pagePath = "/" + pagePath;
						welcomePages.add(pagePath);
					}
				}
			}
			
			//错误页列表
			@SuppressWarnings("unchecked")
			List<Element> errorPageList = rootElement.elements("error-page");
			for(Element element : errorPageList){
				pagePath = element.elementTextTrim("location");
				if(StringUtils.isNotBlank(pagePath)){
					if(!pagePath.startsWith("/"))
						pagePath = "/" + pagePath;
					errorPages.add(pagePath);
				}
			}
			
			//用户自定义排除页列表
			@SuppressWarnings("unchecked")
			List<Element> contextParams = rootElement.elements("context-param");
			for(Element element : contextParams){
				String paramName = element.elementTextTrim("param-name");
				if(excludeFilterJSPListParamName.equalsIgnoreCase(paramName)){
					String pagePathStrs = element.elementTextTrim("param-value");
					if(StringUtils.isNotEmpty(pagePathStrs)){
						String[] urlArray = pagePathStrs.split(URL_SPLIT_PATTERN);
						for (String url : urlArray) {
							url = url.trim();
							if (url.length() == 0) {
								continue;
							}
							excludePages.add(url);
						}
					}
					break;
				}
			}
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static List<String> getWelcomePages(){
		return welcomePages;
	}
	
	public static List<String> getErrorPages(){
		return errorPages;
	}

	/**
	 * getExcludePages(描述这个方法的作用)<br/>
	 * @return 
	 * Collection<? extends String>
	 * @exception 
	 * @since  1.0.0
	*/
	public static List<String> getExcludePages() {
		return excludePages;
	}
	
}
